<?php
 Session_Start () ;
 If (!$_SESSION ["repertoire_config"]) { Header ("Location: ../../sortie.php?sortie=3") ; }
 Include ($_SESSION ["repertoire_config"]."/garennes.cfg") ;
 Include ($_SESSION ["repertoire_config"]."/init.php") ;
 Include ("../include/config.inc") ;
 //verif_droits ($droit_courant, $_SESSION ["code"]) ;
 $millesime = millesime (0) ;
 
 $resultats = SQL_commande ("SELECT * FROM menus WHERE numero='".$droit_courant."' AND ordre='0'", $id_sql) ;
 $l = SQL_tableau_associatif ($resultats, "") ;
 
 Echo "<HTML><HEAD>" ;
 entete_html ("Garennes - Purge notes", "", 1) ;
 Echo styles ("none", "none", "none", $l ["lien"], "none") ;
 Echo "</HEAD>" ;

 $controles = SQL_commande ("SELECT * FROM controles ORDER BY id_controle", $id_sql) ;
 
 Echo "<BODY BACKGROUND=\"../images/fond.png\" TEXT=\"".$ligne ["couleur_texte"]."\">" ;
 Echo "<CENTER><FONT COLOR=\"#0000FF\">&nbsp;Attribution&nbsp;des&nbsp;identifiants/mots&nbsp;de&nbsp;passe&nbsp;</CENTER>" ;
 $pb = 0 ;

 // Purge des notes non utilisees
 $non = SQL_commande ("SELECT n.id_note, e.nom, e.prenoms, e.code_classe, c.code_classe AS anc_classe, u.nom AS nom_u
                         FROM notes n, eleves e, controles c, utilisateurs u
                        WHERE n.controle=c.id_controle AND n.eleve=e.id_eleve AND e.classe!=c.classe AND c.numen=u.numen
                          AND e.millesime='".$millesime."'", $id_sql) ;
 If (SQL_nbr_lignes ($non) > 0)
  {
   For ($j = 0 ; $j < SQL_nbr_lignes ($non) ; $j++)
    {
     $n = SQL_tableau_associatif ($non, "") ;
     Echo $n ["id_note"].", Eleve : ".$n ["nom"]." ".$n ["prenoms"]." Classe Actuelle : ".$n ["code_classe"].", Ancienne : ".$n ["anc_classe"].", Prof : ".$n ["nom_u"]."<BR>" ;
     $ok = SQL_commande ("DELETE FROM notes WHERE id_note='".$n ["id_note"]."' LIMIT 1", $id_sql) ;
    }
   Echo "<BR>" ;
  }
 
 // Recherche des controles non utilises
 $controles = SQL_commande ("SELECT * FROM controles WHERE millesime='".$millesime."' ORDER BY id_controle", $id_sql) ;
 $table_ctl [0][0] = 0 ;
 For ($i = 0 ; $i < SQL_nbr_lignes ($controles) ; $i++)
  {
   $ctl = SQL_tableau_associatif ($controles, "") ;
   $non = SQL_commande ("SELECT * FROM notes WHERE controle='".$ctl ["id_controle"]."' AND millesime='".$millesime."'", $id_sql) ;
   If (SQL_nbr_lignes ($non) == 0)
    {
     Echo $ctl ["id_controle"]." -> Non utilis&eacute;<BR>" ;
     $table_ctl [0][0] = $table_ctl [0][0] + 1 ;
     $table_ctl [$table_ctl [0][0]][0] = $ctl ["id_controle"] ;
     $table_ctl [$table_ctl [0][0]][1] = $ctl ["numen"] ;
     $table_ctl [$table_ctl [0][0]][2] = 0 ;
    }
  }

 // Recherche des notes en double et essai d'attribution a un controle non utilise
 $notes = SQL_commande ("SELECT * FROM notes WHERE millesime='".$millesime."' ORDER BY id_note", $id_sql) ;
 $table [0][0] = 0 ;
 $ctl_table [0][0] = 0 ;
 For ($i = 0 ; $i < SQL_nbr_lignes ($notes) ; $i++)
  {
   $nt = SQL_tableau_associatif ($notes, "") ;
   $double = SQL_commande ("SELECT * FROM notes WHERE eleve='".$nt ["eleve"]."' AND controle='".$nt ["controle"]."' AND id_note!='".$nt ["id_note"]."' AND millesime='".$millesime."'", $id_sql) ;
   If (SQL_nbr_lignes ($double) > 0)
    {
     $ok = 0 ;
     For ($j = 1 ; $j <= $table [0][0] ; $j++)
      {
       If (($table [$j][0] == $nt ["eleve"]) And ($table [$j][1] == $nt ["controle"]))
        {
         $ok = 1 ;
	}
      }
     If ($ok == 0)
      {
       $table [0][0]++ ;
       $table [$table [0][0]][0] = $nt ["eleve"] ;
       $table [$table [0][0]][1] = $nt ["controle"] ;
       Echo $nt ["id_note"]."->" ;
       For ($j = 0 ; $j < SQL_nbr_lignes ($double) ; $j++)
        {
	 $db = SQL_tableau_associatif ($double, "") ;
         $table [$table [0][0]][$j+3] = $db ["id_note"] ;
         Echo $db ["id_note"]."-" ;
         $pb = 1 ;
	}
       $table [$table [0][0]][2] = SQL_nbr_lignes ($double) ;
       Echo " pour Contr&ocirc;le:".$nt ["controle"]."-eleve:".$nt ["eleve"] ;
       $ok = 0 ;
       For ($j = 1 ; $j <= $ctl_table [0][0] ; $j++)
        {
         If ($ctl_table [$j][0] == $nt ["controle"]) { $ok = 1 ; }
        }
       If ($ok == 0)
        {
	 $ctl_table [0][0]++ ;
	 $ctl_table [$ctl_table [0][0]][0] = $nt ["controle"] ;
	 Echo " Contr&ocirc;le n:".$nt ["controle"] ;
	}
       Echo "<BR>" ;
      }
    }
  }

 // Attribution des notes si possible
 Echo $ctl_table [0][0]."-".$table_ctl [0][0]."<BR>" ;
 For ($i = 1 ; $i <= $ctl_table [0][0] ; $i++)
  {
   $ok = 0 ;
   $controles = SQL_commande ("SELECT * FROM controles WHERE id_controle='".$ctl_table [$i][0]."' LIMIT 1", $id_sql) ;
   $ctl = SQL_tableau_associatif ($controles, "") ;
   Echo $ctl_table [$i][0]." (".$ctl ["numen"].") ?->" ;
   For ($j = 1 ; $j <= $table_ctl [0][0] ; $j++)
    {
     If (($table_ctl [$j][1] == $ctl ["numen"]) And ($table_ctl [$j][2] == 0)) { $ok = $j  ; }
     //Echo $table_ctl [$j][1].", " ;
    }
   Echo " -> " ;
   If ($ok)
    {
     Echo "Attribution du contr&ocirc;le ".$ctl_table [$i][0]."->".$table_ctl [$ok][0]."<BR>" ;
     $table_ctl [$ok][2] = 1 ;
    }
   Else
    {
     Echo " ???<BR>" ;
    }
  }
 
 Echo "</BODY></HTML>" ;
?>